﻿Public Class frmSSLogin
    Dim dbLogin As New dbUser

    Private Sub FrmLogin_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        MDI.tsLabelLoginAs.Text = "Please Login First"
        lockMenu()
    End Sub

    Private Sub lockMenu()
        MDI.tsLogOut.Enabled = False
        MDI.tsChangePassword.Enabled = False
        For Each rowModule As DataRow In MDI.dsModule.Tables(0).Rows
            Dim mdl As String = rowModule("mdlMenuName").ToString()
            For Each itmHeader As ToolStripMenuItem In MDI.MenuStrip.Items
                If (mdl = itmHeader.Name) Then
                    itmHeader.Visible = False
                    If (itmHeader.DropDownItems.Count > 0) Then
                        For Each itm As ToolStripItem In itmHeader.DropDownItems
                            itm.Visible = False
                        Next itm
                    End If
                End If
            Next itmHeader
            For Each itmHeader As ToolStripMenuItem In MDI.MenuStrip.Items
                If (itmHeader.DropDownItems.Count > 0) Then
                    For Each itm As ToolStripItem In itmHeader.DropDownItems
                        If (itm.Name = mdl) Then
                            itm.Visible = False
                        End If
                    Next itm
                End If
            Next itmHeader
        Next rowModule
    End Sub

    Private Sub Login()
        Dim UserName As String = dbLogin.isValidLogin(txtUsername.Text, txtpassword.Text)
        If (UserName <> "") Then
            MDI.tsLoginID.Text = txtUsername.Text.Trim()
            MDI.LoginUserName = UserName
            MDI.tsLabelLoginAs.Text = "Login As " + UserName
            MDI.tsLogOut.Enabled = True
            MDI.tsChangePassword.Enabled = True
            For Each row As DataRow In dbLogin.GetModulePermissionList(txtUsername.Text.Trim()).Tables(0).Rows
                Dim mdl As String = row("Module").ToString()
                For Each itmHeader As ToolStripMenuItem In MDI.MenuStrip.Items
                    If (itmHeader.Name = mdl) Then
                        itmHeader.Visible = True
                    End If
                    If (itmHeader.DropDownItems.Count > 0) Then
                        For Each itm As ToolStripItem In itmHeader.DropDownItems
                            If (itm.Name = mdl) Then
                                itm.Visible = True
                            End If
                        Next itm
                    End If
                Next itmHeader
            Next row
            MDI.dsPermission = dbLogin.GetFormPermissionList(txtUsername.Text.Trim())
            For Each row As DataRow In MDI.dsPermission.Tables(0).Rows
                Dim frm As String = row("MenuName").ToString()
                For Each itmHeader As ToolStripMenuItem In MDI.MenuStrip.Items
                    If (itmHeader.DropDownItems.Count > 0) Then
                        For Each itm As ToolStripItem In itmHeader.DropDownItems
                            If (itm.Name = frm) Then
                                itm.Visible = True
                            End If
                        Next itm
                    End If
                Next itmHeader
            Next row
            Me.Close()
        Else : MessageBox.Show("Invalid Login Account")
        End If

    End Sub

    Private Sub btnLogin_Click(sender As Object, e As EventArgs) Handles btnLogin.Click
        Login()
    End Sub

    Private Sub login_KeyDown(sender As Object, e As KeyEventArgs) Handles txtpassword.KeyDown, txtUsername.KeyDown
        If (e.KeyData = Keys.Enter) Then
            Login()
        End If
    End Sub

    Private Sub cont_Tick(sender As Object, e As EventArgs)

    End Sub

    Private Sub txtpassword_Enter(sender As Object, e As EventArgs) Handles txtpassword.Enter
        txtpassword.SelectAll()
    End Sub
End Class